import React,{ useEffect } from 'react'
import { View,Text,Image,Button } from '@tarojs/components'
import './index.less'
import Taro,{ useDidShow } from '@tarojs/taro';
import { Store } from '@/store/core.store';
import UserStore from '@/store/user.store';
const baseCls = `legions-merge-request-detail`;
import dayjs from 'dayjs'
import MergeRequestStore from '@/store/merge.request.store';
import { routers } from '@/router';
import { mergeRequestService } from '@/api/merge.request.service';
import get from 'lodash/get';
export default function MergeRequestDetail() {
    const user_store = Store.getStore(UserStore)
    const store = Store.getStore(MergeRequestStore)
    useDidShow(() => {
        user_store.authentication();
    })
    useEffect(() => {
        return () => {
            store.setDetail(null);
        }
    },[])
    const onGotoGitlab = () => {
        if (Taro.getEnv() === Taro.ENV_TYPE.WEAPP) {
            Taro.navigateTo({
                url: `${routers.merge_requests}?url=${store.repository_https}`
            })
        }
        if (Taro.getEnv() === Taro.ENV_TYPE.WEB) {
            window.open(store.repository_https)
        }
    }
    const onPushMessage = async () => {
        if (store.detail) {
            const result = await mergeRequestService.planStop(store.detail.merge_request_iid.toString(),store.detail['project_id']);
            if (get(result,'success')) {
                Taro.showToast({
                    title: get(result,'message'),
                    icon: 'success',
                    duration: 2000,
                })
            } else {
                Taro.showToast({
                    title: get(result,'message'),
                    icon: 'error',
                    duration: 2000,
                })
            }
        }

    }
    const onApproved = () => {
        if (store.isDisabledApproved) {
            return;
        }
        Taro.showModal({
            title: '同意',
            content: '合并请求审批通过',
            success: async function (res) {
                if (res.confirm) {
                    const result = await mergeRequestService.approved(store.detail?._id as string,store.detail?.merge_request_iid as unknown as string)
                    if (get(result,'success')) {
                        Taro.showToast({
                            title: get(result,'message'),
                            icon: 'success',
                            duration: 2000,
                        })
                        Taro.navigateTo({
                            url: routers.merge_request_lst
                        })
                    } else {
                        Taro.showToast({
                            title: get(result,'message'),
                            icon: 'error',
                            duration: 2000,
                        })
                    }
                }
            }
        })
    }
    return (
        <View className={baseCls}>
            <View className='panel__content'>
                <View className='title'>申请仓库信息</View>
                <View className='content'>
                    <Text className='li'>
                        来源仓库：{store.detail?.repository_name}
                    </Text>
                    <Text className='li'>
                        来源分支：{store.detail?.source_branch}
                    </Text>
                    <Text className='li'>
                        目标仓库：{store.detail?.target_repository_name}
                    </Text>
                    <Text className='li'>
                        目标分支：{store.detail?.target_branch}
                    </Text>
                </View>
                <View className='title'>申请信息</View>
                <View className='content'>
                    <Text className='title'>
                        合并标题：{store.detail?.merge_title}
                    </Text>
                    <Text className='title desc'>
                        合并描述：{store.detail?.merge_description}
                    </Text>
                    <Text className='li'>
                        流水ID：{store.detail?.merge_request_iid}
                    </Text>
                    <View className='li'>
                        <View className='name'>
                            <Text className='text'>{store.detail?.creator}</Text>
                        </View>
                        <View className='direction'><Image className='image' src='https://hoolinks-frontend-resources.oss-cn-shenzhen.aliyuncs.com/images/weapp-to.png'></Image></View>
                        <View className='name'>
                            <Text className='text'>{store.detail?.assign_members}</Text>
                        </View>
                    </View>
                    <Text className='li'>
                        申请时间：{dayjs(store.detail?.create_time).format('YYYY-MM-DD HH:mm:ss')}
                    </Text>
                    <Text className='li'>
                        处理时间：{store.detail?.audit_status === '1' ? dayjs(store.detail?.update_time).format('YYYY-MM-DD HH:mm:ss') : ''}
                    </Text>

                </View>


            </View>
            <View className='bottom'>
                <Button className='button disabled' disabled={true} style={{ color: '#FF2A2A',border: '1px solid #FF2A2A' }} >删除</Button>
                <Button className='button' style={{ color: '#108DE9',border: '1px solid #0E8FE9' }} onClick={onGotoGitlab}>查看</Button>
                <Button className={`button ${store.isPushMessage ? 'disabled' : ''}`} disabled={store.isPushMessage} style={{ color: '#108DE9',border: '1px solid #0E8FE9' }} onClick={onPushMessage}>推送</Button>
                {/* <Button className={`button disabled`} disabled={true} style={{ color: '#FF9D00',border: '1px solid #FF9D00' }} >驳回</Button> */}
                <Button className={`button ${store.isDisabledApproved ? 'disabled' : ''}`} disabled={store.isDisabledApproved} style={{ color: '#FFFFFF',background: '#0E8FE9' }} onClick={onApproved}>通过</Button>
            </View>
        </View>
    )
}
